<template>
  <div class="container">
    <!-- <div class="container-1200"> -->
    <div class="admin-container">
      <div class="flexWithJusBetween mg-B20">
        <div class="title">项目资料填报</div>
        <div>
          <el-button type="primary" @click="addProject">新增</el-button>
        </div>
      </div>
      <div v-show="queryType == false">
        <el-form ref="warehouseForm" label-width="80px" :inline="true">
          <el-form-item label="关键词">
            <el-input
              prefix-icon="el-icon-search"
              v-model="query.keyword"
              clearable
            ></el-input>
          </el-form-item>
          <el-form-item label="企业名称">
          <el-select v-model="query.companyId" placeholder="请选择企业" clearable>
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in companyList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
          <el-form-item label="填报时间">
            <el-date-picker
              v-model="query.date"
              value-format="yyyy-MM-dd"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </el-form-item>
          <el-button
            style="margin-left: 10px"
            icon="el-icon-search"
            type="primary"
            @click="submitQuery"
            >查询
          </el-button>
          <el-button
            style="margin-left: 10px"
            icon="el-icon-refresh-right"
            type="info"
            @click="resetQuery"
            >重置
          </el-button>
        </el-form>
      </div>

      <el-table :data="tableData" style="width: 100%" border align="center">
        <el-table-column prop="projectName" label="项目名称" align="center">
        </el-table-column>
        <el-table-column prop="compnayName" label="企业名称" align="center">
        </el-table-column>
        <el-table-column prop="startDate" label="启动时间" align="center">
        </el-table-column>
        <el-table-column
          prop="linkman"
          label="负责人"
          align="center"
          
        >
        </el-table-column>
        <el-table-column
          prop="phone"
          label="负责人联系方式"
          align="center"
          
        >
        </el-table-column>
        <el-table-column prop="createDate" label="填报时间" align="center">
        </el-table-column>
        <el-table-column label="项目状态" align="center" >
          <template slot-scope="scope">
            <div>
              {{ projectStatusFormate("project_status-" + scope.row.status) }}
            </div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <el-button @click="viewProject(scope.row)" type="text" size="small"
              >查看</el-button
            >
            <el-button type="text" size="small" @click="editProject(scope.row)" v-if="scope.row.status==0||scope.row.status==3" 
              >修改</el-button
            >
            <el-button type="text" size="small" @click="applyUpdate(scope.row)" v-if="scope.row.status==2"
              >申请订正</el-button
            >
            <!-- <el-button type="text" size="small" @click="projectExecute(scope.row)"
              >项目运营填报</el-button
            > -->
          </template>
        </el-table-column>
      </el-table>
      <el-footer class="el-footerBox">
        <el-pagination
          background
          layout="prev, pager, next"
          :total="total"
          :page-size="pageSize"
          :current-page.sync="pageNum"
          @current-change="getProjectList"
        >
        </el-pagination>
      </el-footer>
    </div>
  </div>
</template>

<script>
import { validUsername } from "@/utils/validate";
import utils from "@/utils/utils.js";
import { find as companyFind,list as companyList } from "@/api/company";
import { list as projectList, update as projectUpdate } from "@/api/project";

export default {
  components: {},
  name: "project",
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error("密码不能少于6位"));
      } else {
        callback();
      }
    };
    return {
      queryType: false,
      query: {
        keyword: "",
        date:"",
        startDate:"",
        endDate:"",
        companyId: "",
      },
      total: 0,
      pageNum: 1,
      pageSize: 20,
      tableData: [],
      companyInfo: "",
      companyList:[],

      //字典信息
      projectStatusDict: [],

      projectStatusDictPromise: "",
    };
  },
  watch: {
    $route: {
      handler: function (route) {},
      immediate: true,
    },
  },

  created() {
    this.projectStatusDictPromise = utils.getDictList(
      "project_status",
      (res) => {
        this.projectStatusDict = res.data;
      }
    );
  },

  mounted() {
    // if (this.$route.query) {
    //   this.companyId = this.$route.query.companyId;
    //   // this.companyFind(this.companyId);
    // }
    let _this = this;
    _this.getCmpanyList()
    Promise.all([this.projectStatusDictPromise]).then((res) => {
      //console.log("字典已经全部加载完成--");
      this.getProjectList();
    });
  },

  destroyed() {},

  methods: {
    //获取项目列表
    getProjectList() {
      let _this = this;
      let params = new URLSearchParams();
      params.append("companyId", _this.query.companyId);
      params.append("startDate", _this.query.startDate);
      params.append("endDate", _this.query.endDate);
      params.append("keyword", _this.query.keyword);
      params.append("pageNum", _this.pageNum);
      params.append("pageSize", _this.pageSize);
      projectList(params)
        .then((res) => {
          console.log("ProjectList成功获取数据--", res);
          if (res.type == "success") {
            this.tableData = res.data.data;
            this.total = res.data.total;
            if (this.tableData.length > 0) {
              this.tableData.map((item) => {
                item.startDate = utils.dateFormatter(item.startDate);
                item.createDate = utils.dateFormatter(item.createDate);
                
              });
            }
          }
        })
        .catch((err) => {
          this.$message.error(err.msg);
        });
    },

    applyUpdate(data){
      console.log("data--", data);
      data.status = 4
      projectUpdate(data)
            .then((res) => {
              console.log("companySave成功获取数据--", res);
              if (res.type == "success") {
                this.getProjectList();
              }
            })
            .catch((err) => {
              this.$message.error(err.msg);
            });
    },

    //条件查询
    submitQuery() {
      console.log('this.query.date--',this.query);
      if(!utils.isNull(this.query.date)){
        this.query.startDate = this.query.date[0]
        this.query.endDate = this.query.date[1]
      }
      this.getProjectList();
    },

    //条件查询
    resetQuery() {
      this.query = {
        keyword: "",
        date:"",
        startDate:"",
        endDate:"",
        companyId:""
      },
        this.getProjectList();
    },

    //新增项目
    addProject() {
      this.$store.dispatch("user/setCloseAliveName", 'projectAdd')
      this.$router.push({
        path: "/project/add",
        query: { companyId: this.companyId },
      });
    },

    //查看项目
    viewProject(data) {
      this.$store.dispatch("user/setCloseAliveName", 'projectView')
      this.$router.push({
        path: "/project/view",
        query: { projectId: data.id },
      });
    },

    //编辑项目
    editProject(data) {
      this.$store.dispatch("user/setCloseAliveName", 'projectEdit')
      this.$router.push({
        path: "/project/edit",
        query: { projectId: data.id, companyId: this.companyId },
      });
    },

     //获取公司列表
    getCmpanyList() {
      companyList().then((res) => {
        console.log("获取公司信息res--", res);
        if (res.type == "success") {
          this.companyList = res.data.data;
        }
      });
    },

    //获取公司信息
    companyFind(id) {
      companyFind(id).then((res) => {
        console.log("获取公司信息res--", res);
        if (res.type == "success") {
          this.companyInfo = res.data.company;
        }
      });
    },

    projectStatusFormate(data) {
      return utils.selectDictLabel(this.projectStatusDict, data);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-pagination {
  display: block;
}
</style>
